function render(){
  $.ajax({
  url: "http://localhost:3000/commonts",
  type: "GET",
  success: function(data){
      console.log(data)
      $("#ul").html(template("myTemplate", {'commonts':data}));
  }
  })  
}

render();

// 添加评论
$("#addContentBtn").click(function(){
  //获取昵称和评论内容
  let nickName = $("#nickName").val();
  let content = $("#content").val();
  if(!nickName || !content){
      alert("请将信息填写完整");
      return;
  }
  let obj = {
      nickName, content, dateTime:new Date()
  }
  console.log(obj);
  //添加评论信息
  $.ajax({
      url: "http://localhost:3000/commonts",
      type: "POST",
      data: obj,
      success: function(data){
          render();
          $("#nickName").val("");
          $("#content").val("");
          setTimeout(()=>{
              alert("发表成功！");
          },100);
      }
  }) 
})

// 删除评论
function deleteCommont(id){
  if(confirm("确定删除吗？")){
      $.ajax({
          url: "http://localhost:3000/commonts/"+id,
          type: "DELETE",
          data: {
              "id": id
          },
          success: function(data){
              render();
          }
      })
  }  
}